#!/usr/bin/env python
import codecs
import types
import urllib
import sys
reload(sys)
sys.setdefaultencoding('utf-8')



def encode_code(mystr,code='utf-8'):
    if type(mystr) is types.UnicodeType:
        try:
            decode_str=codecs.getdecoder('gb2312')(mystr)[0]
        except UnicodeDecodeError,e:
            try:
                decode_str=codecs.getdecoder('gbk')(mystr)[0]
            except UnicodeDecodeError,e:
                try:
                    decode_str=codecs.getdecoder('utf-8')(mystr)[0]
                except UnicodeDecodeError,e:
                    try:
                        decode_str=codecs.getdecoder('gb18030')(mystr)[0]
                    except UnicodeDecodeError,e:
                        try:
                            decode_str=codecs.getdecoder('base5')(mystr)[0]
                        except UnicodeDecodeError,e:
                            try:
                                decode_str=codecs.getdecoder('big5')(mystr)[0]
                            except UnicodeDecodeError,e:
                                try:
                                    decode_str=codecs.getdecoder('unicode')(mystr)[0]
                                except UnicodeDecodeError,e:
                                    return
        encode_str=codecs.getencoder(code)(decode_str)[0]
        return encode_str
    else:
        return mystr
    
def unite_code(mystr,code='utf-8'):
    if type(mystr) is types.UnicodeType:
        try:
            decode_str=urllib.unquote(mystr).decode('gb2312')
        except UnicodeDecodeError,e:
            try:
                decode_str=urllib.unquote(mystr).decode('gbk')
            except UnicodeDecodeError,e:
                try:
                    decode_str=urllib.unquote(mystr).decode('utf-8')
                except UnicodeDecodeError,e:
                    try:
                        decode_str=urllib.unquote(mystr).decode('gb18030')
                    except UnicodeDecodeError,e:
                        try:
                            decode_str=urllib.unquote(mystr).decode('base5')
                        except UnicodeDecodeError,e:
                            try:
                                decode_str=urllib.unquote(mystr).decode('big5')
                            except UnicodeDecodeError,e:
                                try:
                                    decode_str=urllib.unquote(mystr).decode('unicode')
                                except UnicodeDecodeError,e:
                                    return None
        encode_str=decode_str.encode(code)
        return urllib.quote(encode_str)
        
    else:
        return mystr
    
def decode_code(mystr,code='utf-8'):
    if None==mystr:
        return None
    try:
        decode_str=urllib.unquote(mystr).decode(code)
        return decode_str
    except UnicodeDecodeError,e:
        pass
	#return None
    
def convert_key(key,code='utf-8',unite_code='utf-8'):
    keyword_list = []
    code_list = []
    code_k = None
    try:
        #delete + in original_keyword
        lenth = len(key)
        res = key.find("\n")
        if -1 != res:
            new_keyword = key[0:res]
        else:
            new_keyword = key
        tmp_list = new_keyword.split("+")
        for tmp_str in tmp_list:
            #delete ? in tmp_str
            tmp_list2 = tmp_str.split('?')
            for t in tmp_list2:
                if None == t:
                    continue
                keyword_list.append(t)
        for k in keyword_list:
            if None == k:
                continue
            else:
                res = 0
                res = k.find('%')
                if -1 == res:
                    code_list.append(k)
                    continue
                else:
                    ch = k[0:res]
                    code_list.append(ch)
                    if unite_code != code:
                        tmp_res = unite_code(k[res:len(k)])
                        result = decode_code(tmp_res)
                        code_list.append(result)
                    else:
                        result = decode_code(k[res:len(k)])
                        code_list.append(result)
        for k in code_list:
            if code_k == None:
                code_k = k
            else:
                if None == k:
                    continue
                code_k += k
        return code_k
    except Exception,e:
        #print key,"--",e,"\n"
        return key

